package com.car.android.sdk.max;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdRevenueListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.car.android.sdk.Constant;
import com.car.android.sdk.CarAdSdk;
import com.car.android.sdk.ad.CarVideoIAP;


public class CarMaxReward implements CarVideoIAP {
    private static CarMaxReward adManager;
    private MaxRewardedAd maxRewardedAd;
    private double mEcpm=0;

    public static CarMaxReward getInstance() {
        if (adManager == null) {
            adManager = new CarMaxReward();
        }
        return adManager;
    }

    private MaxRewardedAd getRewardedAd(Context context,String adUnitId) {
        return MaxRewardedAd.getInstance(adUnitId, context);
    }

    public void loadRewardAd(Context context) {
        if (maxRewardedAd == null) {
            maxRewardedAd =getRewardedAd(context, Constant.REWARD_PLACEMENTID);
            setListner(null);
            loadRewardAd();
        } else {
            if (!maxRewardedAd.isReady()) {
                loadRewardAd();
            }
        }
    }

    private void loadRewardAd(){
        mEcpm=0;
        maxRewardedAd.loadAd();
    }


    private void setListner(CarAdSdk.OnAdShowListner listner) {
        maxRewardedAd.setListener(new MaxRewardedAdListener() {

            @Override
            public void onUserRewarded(MaxAd ad, com.applovin.mediation.MaxReward reward) {
//                Log.e("CCMatchW","onUserRewarded");
            }

            @Override
            public void onAdLoaded(MaxAd ad) {
                mEcpm=ad.getRevenue();
                Log.d("CCMatchW","onMaxRewardLoaded:"+mEcpm);
            }

            @Override
            public void onAdDisplayed(MaxAd ad) {
                Log.e("CCMatchW","onAdDisplayed"+ad.getNetworkName());
            }

            @Override
            public void onAdHidden(MaxAd ad) {
                loadRewardAd();
                if (listner != null) {
                    listner.reward(true);
                }
            }

            @Override
            public void onAdClicked(MaxAd ad) {
            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                mEcpm=0;
                Log.d("CCMatchW","onMaxRewardLoadFailed "+error.getCode()+ error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                Log.e("CCMatchW","onAdDisplayFailed "+error.getCode() +error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
                if (listner != null) {
                    listner.error(String.valueOf(error.getCode()));
                }
            }
        });
        maxRewardedAd.setRevenueListener(new MaxAdRevenueListener() {
            @Override
            public void onAdRevenuePaid(MaxAd ad) {
                if (listner != null) {
                    listner.startPlay();
                }
            }
        });
    }

    public void showRewardedVideo(Context context, CarAdSdk.OnAdShowListner listner) {
        if (maxRewardedAd == null) {
            maxRewardedAd =getRewardedAd(context,Constant.REWARD_PLACEMENTID);
        }
        if (maxRewardedAd.isReady()) {
            setListner(listner);
            maxRewardedAd.showAd();
        } else {
            setListner(listner);
            loadRewardAd();
            if (listner!=null){
                listner.error("6000");
            }
        }
    }

    @Override
    public void showVideo(Activity activity, CarAdSdk.OnAdShowListner listner) {
        showRewardedVideo(activity, listner);
    }

    @Override
    public double getEcpm() {
        return mEcpm;
    }
}
